//
//  OMURL.m
//  RequestTest
//
//  Created by xntech on 13-8-19.
//  Copyright (c) 2013年 xntech. All rights reserved.
//

#import "OMURL.h"
#import "OMHTTPConstant.h"
@interface OMURL()
@property (nonatomic,copy)NSString  *absoluteUrl;
@end
@implementation OMURL
@synthesize absoluteUrl = _absoluteUrl;
+ (NSMutableDictionary *)parseUrl:(NSString *)urlString
{
    NSScanner *scanner = [NSScanner scannerWithString:urlString];
    [scanner setCharactersToBeSkipped:[NSCharacterSet characterSetWithCharactersInString:@"&?"]];
    NSString *tempString;
    NSMutableArray *vars = [NSMutableArray array];
    [scanner scanUpToString:@"?" intoString:nil];       //ignore the beginning of the string and skip to the vars
    while ([scanner scanUpToString:@"&" intoString:&tempString]) {
        [vars addObject:[tempString copy]];
    }
    
    NSMutableDictionary  *resultDic = [NSMutableDictionary dictionary];
    for(NSString *str in vars)
    {
        NSArray  *component = [str componentsSeparatedByString:@"="];
        if ([component count] == 2)
        {
            [resultDic setObject:[component objectAtIndex:1] forKey:[component objectAtIndex:0]];
        }
    }
    return resultDic;
}

- (id)initWithAbsoluteString:(NSString *)absUrl
{
    self = [super init];
    if (self)
    {
        self.absoluteUrl = absUrl;
        _params = [[NSMutableDictionary alloc] init];
    }
    return self;
}

- (void)addParam:(id)object forKey:(id)key
{
    [_params setObject:[object description] forKey:[key description]];
}
- (void)addIntParam:(int)value forKey:(id)key
{
    NSNumber  *valueNum = [NSNumber numberWithInt:value];
    [self addParam:valueNum forKey:key];
}
- (void)addLongParam:(long long)value forKey:(id)key
{
    NSNumber  *valueNum = [NSNumber numberWithLongLong:value];
    [self addParam:valueNum forKey:key];
}
- (void)addDoubleParam:(double)value forKey:(id)key
{
    NSNumber  *valueNum = [NSNumber numberWithDouble:value];
    [self addParam:valueNum forKey:key];
}
- (void)addBoolParam:(BOOL)value forKey:(id)key
{
    NSString  *valueStr = nil;
    if (value) {
        valueStr = OMTRUE;
    }else{
        valueStr = OMFALSE;
    }
    [self addParam:valueStr forKey:key];
}
- (void)addDateParam:(NSDate *)date forKey:(id)key
{
    NSTimeInterval interval = [date timeIntervalSince1970];
    interval = interval * 1000;
    [self addLongParam:interval forKey:key];
}
- (void)setParams:(NSDictionary *)dic
{
    [_params removeAllObjects];
    [self addParams:dic];
}
- (void)addParams:(NSDictionary *)dic
{
    [_params addEntriesFromDictionary:dic];
}
- (NSString  *)relativeUrlString
{
    NSMutableString *relativeString = [NSMutableString string];
    for(NSString *key in [_params allKeys])
    {
        [relativeString appendFormat:@"%@=%@&",key,[_params objectForKey:key]];
    }
    return relativeString;
}
- (NSString  *)urlString
{
    NSMutableString  *url = [NSMutableString stringWithString:self.absoluteUrl];
    if ([url hasSuffix:@"?"]) {
        [url appendString:[self relativeUrlString]];
    }else{
        [url appendFormat:@"?%@",[self relativeUrlString]];
    }
    return url;
}
- (NSURL *)URL
{
    return [NSURL URLWithString:[self urlString]];
}
- (NSString *)description
{
    return [self urlString];
}
- (void)dealloc
{
    [_absoluteUrl release];
    [_params release];
    [super dealloc];
}
@end
